package com.kakaxi.mybilibili.module.home.live.presenter;

import android.util.SparseArray;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.LiveItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.live.model.ILiveCategoryRoomsModel;
import com.kakaxi.mybilibili.module.home.live.model.LiveCategoryRoomsModel;
import com.kakaxi.mybilibili.module.home.live.view.ILiveCategoryRoomsView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 分区直播间控制层实现类
 * Created by kakaxi on 16/11/17.
 */

public class LiveCategoryRoomsPresenter implements ILiveCategoryRoomsPresenter {

    /** 数据层 **/
    private ILiveCategoryRoomsModel mModel;
    /** 视图层 **/
    private ILiveCategoryRoomsView mView;

    public LiveCategoryRoomsPresenter(ILiveCategoryRoomsView liveCategoryRoomsView) {
        mView = liveCategoryRoomsView;
        mModel = new LiveCategoryRoomsModel();
    }

    @Override
    public void initCategoryRoomsTag() {
        mModel.getCategoryRoomsTagData(new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) { // 成功
                    SparseArray<List<String>> tagSparseArr = parseCategoryRoomsTagData(response);
                    mView.showCategoryRoomsTag(tagSparseArr);
                } else { // 失败
                    mView.showMessage(response.getString(WebParamConst.RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
            }
        });
    }

    @Override
    public void initCategoryRoomsList(int areaCategoryID, int page, String sort, String tag) {
        mView.showLoading();
        mView.clearRoomsList();
        getCategoryRoomsListData(areaCategoryID, page, sort, tag, false);
    }

    @Override
    public void refreshCategoryRoomsList(int areaCategoryID, int page, String sort, String tag) {
        mView.clearRoomsList();
        getCategoryRoomsListData(areaCategoryID, page, sort, tag, false);
    }

    @Override
    public void getMoreCategoryRoomsList(int areaCategoryID, int page, String sort, String tag) {
        getCategoryRoomsListData(areaCategoryID, page, sort, tag, true);
    }


    /**
     * 解析标签数据
     *
     * @param response 响应数据
     * @return 标签数组
     */
    private SparseArray<List<String>> parseCategoryRoomsTagData(JSONObject response) {
        SparseArray<List<String>> tagSparseArr = new SparseArray<>();
        JSONObject dataJsonObj = response.getJSONObject(WebParamConst.RESPONSE_DATA);

        Set<Map.Entry<String, Object>> allTagArrSet = dataJsonObj.entrySet();
        for (Map.Entry<String, Object> singleTagArrEntry : allTagArrSet) {
            int key = -1;
            try {
                key = Integer.valueOf(singleTagArrEntry.getKey());
            } catch (NumberFormatException ex) {
                ex.printStackTrace();
            }
            Object singleTagJsonArr = singleTagArrEntry.getValue();
            // 键值只能是数字, json数组不能空也不能为空字符串
            if (key != -1 && singleTagJsonArr != null && !singleTagJsonArr.toString().trim()
                    .equals("")) {
                List<String> singleTagList = JSON.parseArray(singleTagJsonArr.toString(), String
                        .class);
                tagSparseArr.put(key, singleTagList);
            }
        }
        return tagSparseArr;
    }

    private void getCategoryRoomsListData(int areaCategoryID, int page, String sort, String tag, final boolean isMore) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put(ILiveCategoryRoomsModel.PARAM_AREA_ID, String.valueOf(areaCategoryID));
        paramMap.put(ILiveCategoryRoomsModel.PARAM_PAGE, String.valueOf(page));
        paramMap.put(ILiveCategoryRoomsModel.PARAM_SORT, sort);
        if (!tag.equals("全部")) {
            paramMap.put(ILiveCategoryRoomsModel.PARAM_TAG, tag);
        }
        mModel.getCategoryRoomsListData(paramMap, new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();

                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) { // 成功
                    String dataArrStr = response.getJSONArray(WebParamConst.RESPONSE_DATA)
                            .toString();
                    List<LiveItemBean> liveItemBeanList = JSON.parseArray(dataArrStr,
                            LiveItemBean.class);

                    if (isMore) {
                        mView.showMoreCategoryRoomsList(liveItemBeanList);
                    } else {
                        mView.showCategoryRoomsList(liveItemBeanList);
                    }
                } else { // 失败
                    mView.showMessage(response.getString(WebParamConst.RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }


}
